<template>
  <div>
    <upload-excel :on-success="onSuccess" />
  </div>
</template>

<script>
import { importEmployees } from '@/api/employees.js'
import { formatDate } from '@/utils'
export default {
  data() {
    return {

    }
  },

  created() {

  },

  methods: {
    async onSuccess({ results }) {
      const userRelations = {
        '入职日期': 'timeOfEntry',
        '手机号': 'mobile',
        '姓名': 'username',
        '转正日期': 'correctionTime',
        '工号': 'workNumber'
      }
      // 可以根据循环的数组 返回一个新数组 而且长度一致 内容来自于return
      // 处理数组数据格式化 [{}, {}, {}] [1,1,1]
      console.log(results)
      const excelArr = results.map(item => {
        const obj = {}
        // ['入职日期', '手机号', '姓名', '转正日期', '工号']
        Object.keys(item).forEach(key => {
          if (key === '入职日期' || key === '转正日期') {
            obj[userRelations[key]] = formatDate(item[key], '/')
            return false
          }
          obj[userRelations[key]] = item[key]
        })
        return obj
      })
      await importEmployees(excelArr)
      this.$message.success('导入成功')
      this.$router.back()
    }
  }
}
</script>

<style scoped lang='less'>

</style>
